#include "task/retinaface.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include "task/retinaface.h"

int main() {
    // 1. 创建 VideoCapture 对象并打开视频文件或摄像头
    //std::string videoFile = "your_video.mp4";  // 替换为你的视频文件路径
    // cv::VideoCapture cap(videoFile);
    RetinafaceCustom retinaface;
    retinaface.LoadModel("/mnt/sdcard/retinaface_lib/data/model/retinaFace_mobilenet640_fp.rknn");

    // 如果要从摄像头捕获视频，可以使用以下代码替代上面的代码
    cv::VideoCapture cap("/dev/video0");  // 0 是摄像头的设备编号

    // printf("read video successfully!!!");
    // 检查视频是否成功打开
    if (!cap.isOpened()) {
        std::cerr << "无法打开视频文件或摄像头" << std::endl;
        return -1;
    }

    // 获取视频的帧率
    double fps = cap.get(cv::CAP_PROP_FPS);
    std::cout << "视频帧率: " << fps << " FPS" << std::endl;

    //2. 创建窗口
    std::string windowName = "Video Display";
    cv::namedWindow(windowName, cv::WINDOW_NORMAL);

    // 3. 读取视频帧并显示
    while (true) {
        cv::Mat frame;
        
        // 读取视频帧
        bool isSuccess = cap.read(frame);
        
        // 检查是否成功读取帧
        if (!isSuccess) {
            std::cout << "视频读取结束或读取帧失败" << std::endl;
            break;
        }

        retinaface_result result;
        retinaface.Run(frame, &result);

        // printf("result count : %d", result.count);
        for (int i = 0; i < result.count; ++i) {
            int x1 = result.object[i].box.left;
            int y1 = result.object[i].box.top;
            int x2 = result.object[i].box.right;
            int y2 = result.object[i].box.bottom;
            cv::Rect bbox(x1, y1, x2-x1, y2-y1);
      
            cv::rectangle(frame, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 0, 255), 3);
            for(int j=0; j< 5; j++)
            {
                cv::circle(frame, cv::Point(result.object[i].ponit[j].x, result.object[i].ponit[j].y), 4, cv::Scalar(0, 0, 255), 2, cv::LINE_AA, 0);
            }
        }

        // 显示帧
        cv::imshow(windowName, frame);

        // 等待按键按下，按下 'q' 键退出
        char c = (char)cv::waitKey(1000 / fps);
        if (c == 'q' || c == 27) {  // 27 是 ESC 键的 ASCII 码
            std::cout << "退出视频播放" << std::endl;
            break;
        }
    }

    // 4. 释放 VideoCapture 对象并关闭所有窗口
    cap.release();
    cv::destroyAllWindows();

    return 0;
}
